package com.ctms.system.controller;

import com.ctms.common.utils.PageUtils;
import com.ctms.common.utils.Query;
import com.ctms.common.utils.ShiroUtils;
import com.ctms.system.domain.CourseStuAmountDO;
import com.ctms.system.domain.TaskReleaseDO;
import com.ctms.system.domain.UserDO;
import com.ctms.system.service.CourseStudentService;
import com.ctms.system.service.TaskReleaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@RequestMapping("/ShowCommitTask")
@Controller
//展示每个任务的学生提交情况
public class ShowCommitTaskController {

    @Autowired
    TaskReleaseService taskReleaseService;
    @Autowired
    CourseStudentService courseStudentService;

    @GetMapping("")
    public String show(){
        return "sys/taskRelease/showCommitTask";
    }

    @GetMapping("/list")
    @ResponseBody
    PageUtils list(@RequestParam Map<String,Object> params){
        //获取登录用户信息
        UserDO userDO = ShiroUtils.getUser();
        String name = userDO.getName();
        //获取前台的参数
        params.put("name",name);
        Query query = new Query(params);
        //查询当前教师的所有实训任务
        List<TaskReleaseDO> taskReleaseDOS = taskReleaseService.selectAllCommitTask(params);
        //查询每门课的选课人数
        List<CourseStuAmountDO> courseStuAmountDOS = courseStudentService.countAmount();
        for (int i=0;i<taskReleaseDOS.size();i++){
            //已提交人数
            int sum = taskReleaseService.totalCommit(taskReleaseDOS.get(i).getTid());
            taskReleaseDOS.get(i).setIsCommit(sum);
            for (int j=0;j<courseStuAmountDOS.size();j++){
                if (courseStuAmountDOS.get(j).getCourseName().equals(taskReleaseDOS.get(i).getTaskCourse())){
                    //总人数
                    int num = courseStuAmountDOS.get(j).getAmount();
                    //未提交人数
                    taskReleaseDOS.get(i).setNotCommit(num-sum);
                }
            }
        }
        int total = taskReleaseService.studentCommitCount(query);
        //返回结果
        PageUtils pageUtils = new PageUtils(taskReleaseDOS,total);
        return pageUtils;
    }


}
